package com.example.service;

import com.alibaba.fastjson.JSON;
import com.example.entity.Order;
import com.example.repository.StockRepository;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author shenxiang
 * @date 2020/11/14 21:36
 * @description
 */
@Service
@RocketMQMessageListener(topic = "seckill_queue_payed",consumerGroup = "seckill_queue_payed_grp")
public class payedSuccessConsumer implements RocketMQListener<String> {

    @Autowired
    private OrderService orderService;

    @Autowired
    private StockRepository stockRepository;

    @Override
    public void onMessage(String orderId) {
        System.out.println("支付成功队列收到了消息:"+orderId);
        if (!stockRepository.existsOrderNo(orderId)){
            // 订单不存在
            System.out.println("订单不存在");
            return;
        }
        if (stockRepository.existsCancelledOrderNo(orderId)){
            System.out.println("订单:"+orderId+"已经取消了.");
            return;
        }
        if (stockRepository.existsPayedByOrderNo(orderId)){
            System.out.println("订单:"+orderId+"已经支付过了.");
            return;
        }
        orderService.setPayedByOrderNo(orderId);
    }
}
